<%@ page import="java.net.URLDecoder" %>
<%@ page import="com.afishing.entity.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="login-container">
    <%
        ArrayList users = (ArrayList) application.getAttribute("users");
        if (users == null) {
            users = new ArrayList();
            //添加一些测试数据
//            User user = new User(1,"admin", "admin");
//            User user1 = new User(2, "user", "user");
//            users.add(user);
//            users.add(user1);
            application.setAttribute("users", users);
        }
        ArrayList msgs = (ArrayList) application.getAttribute("msgs");
        if (msgs == null) {
            msgs = new ArrayList();
            //添加一些消息样例
//            String msg = "请先登录";
//            String msg1 = "用户名或密码错误";
//            String msg2 = "注册成功";
//            msgs.add(msg);
//            msgs.add(msg1);
//            msgs.add(msg2);
            application.setAttribute("msgs", msgs);
        }
    %>





    <h2>登录</h2>
    <%
        String username = "";
        String password = "";
        String rememberMe = "";
        Cookie[] cookies = request.getCookies();
        //不判空的话，jsp页面会出现空指针异常
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                if ("username".equals(cookie.getName())) {
                    username = URLDecoder.decode(cookie.getValue(), "UTF-8");
                }
                if ("password".equals(cookie.getName())){
                    password = URLDecoder.decode(cookie.getValue(), "UTF-8");
                }
                if ("rememberMe".equals(cookie.getName())){
                    rememberMe = cookie.getValue();
                }
            }
        }

    %>
    <form action="login-servlet" method="POST" onsubmit="return validateLoginForm()">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" value="<%=username!=null? username:""%>">

        <label for="password">Password:</label>
        <input type="password" id="password" name="password" value="<%=password!=null? password:""%>">

        <div class="remember-me">
            <input type="checkbox" id="rememberMe" name="rememberMe" <%= rememberMe.equals("on") ? "checked" : "" %>>
            <label for="rememberMe">Remember me</label>
        </div>

        <button type="submit">Login</button>
    </form>

    <%-- 出错显示的信息框   --%>
    <c:if test="${login_msg != null}">
        <%--  警示请先登录      --%>
        <div class="error-msg">
            <span>${login_msg}</span>
        </div>
    </c:if>
<%--    注册--%>
    <div>
        <a href="register.jsp">没有账号？点击注册</a>
    </div>

</div>
</body>
<script>
    function validateLoginForm() {
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        var usernamePattern = /^[\u4e00-\u9fa5a-zA-Z0-9]+$/;
        var passwordPattern = /^[0-9]+$/;

        if (username === "" || password === "") {
            alert("用户名和密码不能为空");
            return false;
        }
        if (!usernamePattern.test(username)) {
            alert("用户名应为中文、字母或数字的组合");
            return false;
        }
        if (!passwordPattern.test(password)) {
            alert("密码应为纯数字");
            return false;
        }
        if (username.length < 3 || username.length > 20) {
            alert("用户名长度应在3到20个字符之间");
            return false;
        }
        if (password.length < 6 || password.length > 20) {
            alert("密码长度应在6到20个字符之间");
            return false;
        }
        return true;
    }
</script>
</html>
